package de.openkanban.client.ui.presenter;

import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.tile.TileRecord;

import de.openkanban.client.ui.footer.BoardContactTileGrid;
import de.openkanban.client.util.RootPanel;
import de.openkanban.client.util.StringUtils;

/**
 * Callback that handles delete board performer request
 */
public class DeleteBoardPerformerCallback implements BooleanCallback {

	final Canvas source;

	public DeleteBoardPerformerCallback(Object source) {
		super();
		this.source = (Canvas) source;
	}

	public void execute(Boolean value) {
		if (value != null && value) {

			Long currentBoardId = RootPanel.main.getBoard().getWhiteBoardView().getBoardDto().getBoardId();
			BoardContactTileGrid bctg = (BoardContactTileGrid) source;
			TileRecord tr = bctg.getSelectedRecord();
			String userId = tr.getAttribute("userId");

			if (StringUtils.isNumeric(userId) && currentBoardId != null && currentBoardId > 0) {
				PreparedRequest prepReq = new PreparedRequest("board", currentBoardId + "/contact/remove/" + userId);
				prepReq.sendAsyncRequest(new RemoveContactToBoardRequestCallback());
			}
		}
	}
}